<div class="row">
    <div class="col-md-12">
        <div class="portlet">
            <div class="portlet-title">
                <div class="caption">
                    <i class="fa fa-search"></i>
                    <strong><?=__('Customers Search')?></strong>
                </div>
            </div>
            <div class="portlet-body">
                <form class="form-search form-inline">
                    <p style="line-height:350%;">
                        <span class="inline-block">
                            <strong><?=__('Nickname')?>:</strong>
                            <input type="text" class="form-control" name="nickname" size="10" value="<?=$filter['nickname']?>" />
                            &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong><?=__('Type')?>:</strong>
                            <select name="type_id" class="form-control">
                                <option value="0"><?=__('Select...')?></option>
                                <? foreach ($types as $t): ?>
                                <option value="<?=$t['type_id']?>" <?=on($filter['type_id'] == $t['type_id'], 'selected')?>>
                                    <?=OaCustomerTypes::getName($t['type_id'])?>
                                </option>
                                <? endforeach; ?>
                            </select>
                        &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong>Email:</strong>
                            <input type="text" class="form-control" name="email" size="15" value="<?=$filter['email']?>" />
                            &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong><?=__('Mobile')?>:</strong>
                            <input type="text" class="form-control" name="mobile" size="8" value="<?=$filter['mobile']?>" />
                            &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong><?=__('Phone')?>:</strong>
                            <input type="text" class="form-control" name="phone" size="8" value="<?=$filter['phone']?>" />
                            &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong><?=__('Country')?>:</strong>
                            <select name="country_id" class="form-control input-small">
                                <option value="0"><?=__('Select...')?></option>
                                <? foreach ($countries as $country_id): ?>
                                <option value="<?=$country_id?>" <?=on($filter['country_id'] == $country_id, 'selected')?>>
                                    <? $country_en = OaCountries::getName($country_id, 'en-us'); ?>
                                    <?=on($lang === 'zh-cn', strtoupper($country_en[0]). ' - ')?><?=__($country_en)?>
                                </option>
                                <? endforeach; ?>
                            </select>
                        &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong><?=__('Register At')?>:</strong>
                            <input type="text" class="form-control date-picker" name="reg_time_from" size="8" value="<?=$filter['reg_time_from']?>" />
                            -
                            <input type="text" class="form-control date-picker" name="reg_time_to" size="8" value="<?=$filter['reg_time_to']?>" />
                        &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong>IB:</strong>
                            <input type="text" class="form-control" name="merchant_id" size="8" value="<?=on($filter['merchant_id'], $filter['merchant_id'])?>" />
                            &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong>UID:</strong>
                            <input type="text" class="form-control" name="web_uid" size="8" value="<?=on($filter['web_uid'], $filter['web_uid'])?>" />
                            &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong>DEMO:</strong>
                            <input type="text" class="form-control" name="mt4_demo" size="8" value="<?=on($filter['mt4_demo'], $filter['mt4_demo'])?>" />
                            &nbsp;
                        </span>
                        <span class="inline-block">
                            <strong>LIVE:</strong>
                            <input type="text" class="form-control" name="mt4_live" size="8" value="<?=on($filter['mt4_live'], $filter['mt4_live'])?>" />
                            &nbsp;
                        </span>
                        <? if (! OaSales::isStaff($auth->user_id)): ?>
                            <? if (in_array('manager_user_id', $columns)): ?>
                                <span class="inline-block">
                                    <strong><?=__('Leader')?>:</strong>
                                    <select name="manager_user_id" class="form-control input-small">
                                        <option value="0"><?=__('Select...')?></option>
                                        <? foreach ($managers as $user): ?>
                                        <option value="<?=$user['user_id']?>" <?=on($filter['manager_user_id'] == $user['user_id'], 'selected')?>>
                                            <?=OaUsers::getRealName($user)?>
                                        </option>
                                        <? endforeach; ?>
                                    </select>
                                    &nbsp;
                                </span>
                            <? endif; ?>
                            <? if (in_array('sales_user_id', $columns)): ?>
                                <span class="inline-block">
                                    <strong><?=__('Sales')?>:</strong>
                                    <select name="sales_user_id" class="form-control input-small">
                                        <option value="0"><?=__('Select...')?></option>
                                        <? foreach ($sales as $user): ?>
                                        <option value="<?=$user['user_id']?>" <?=on($filter['sales_user_id'] == $user['user_id'], 'selected')?>>
                                            <?=OaUsers::getRealName($user)?>
                                        </option>
                                        <? endforeach; ?>
                                    </select>
                                    &nbsp;
                                </span>
                            <? endif; ?>
                        <? endif; ?>
                        <? if ($action == 'assigned'): ?>
                            <input type="hidden" name="status" value="<?=OaCustomers::ASSIGNED?>" />
                        <? elseif ($action == 'toBeAssigned'): ?>
                            <input type="hidden" name="status" value="<?=OaCustomers::TO_BE_ASSIGNED?>" />
                        <? elseif ($action == 'noFollowUp'): ?>
                            <input type="hidden" name="status" value="<?=OaCustomers::NO_FOLLOW_UP?>" />
                        <? else: ?>
                            <span class="inline-block">
                                <strong><?=__('Status')?>:</strong>
                                <select name="status" class="form-control input-small">
                                    <option value="0"><?=__('Select...')?></option>
                                    <option value="<?=OaCustomers::ASSIGNED?>"       <?=on($filter['status'] == OaCustomers::ASSIGNED, 'selected')?>>      <?=__('Assigned')?></option>
                                    <option value="<?=OaCustomers::TO_BE_ASSIGNED?>" <?=on($filter['status'] == OaCustomers::TO_BE_ASSIGNED, 'selected')?>><?=__('To Be Assigned')?></option>
                                    <option value="<?=OaCustomers::NO_FOLLOW_UP?>"   <?=on($filter['status'] == OaCustomers::NO_FOLLOW_UP, 'selected')?>>  <?=__('No Follow-up')?></option>
                                </select>
                                &nbsp;
                            </span>
                        <? endif; ?>
                        <button type="submit" class="btn green">
                            <i class="fa fa-search"></i>
                            <?=__('Search')?>
                        </button>
                        <input type="hidden" name="col" value="<?=$sortColumn?>" />
                        <input type="hidden" name="dir" value="<?=$sortDirect?>" />
                    </p>
                </form>
            </div>
        </div>
        <div class="portlet">
            <div class="portlet-title">
                <div class="caption">
                    <i class="fa fa-list"></i>
                    <strong><?=__('Search Result')?></strong>
                </div>
                <div class="actions" style="margin-left:10px;">
                    <? if (in_array('add', $actions)): ?>
                        <div class="btn-group">
                            <a class="btn default yellow-stripe" href="<?=url('customer/create')?>" data-toggle="modal" data-target="#customer-modify-modal">
                                <i class="fa fa-plus"></i> <?=__('Add Customer')?>
                            </a>
                        </div>
                    <? endif; ?>
                </div>
                <? partial_view('pagination/small', array('extraClass' => 'pagination-sm')) ?>
            </div>
            <div class="portlet-body">
                <div class="row margin-bottom-5">
                     <div class="col-md-4 col-sm-12">
                        <? if (in_array('assign', $actions)): ?>
                        <button type="button" class="btn blue" data-remote="<?=url('customer/assign')?>" data-toggle="modal" data-target="#customer-assign-modal">
                            <i class="fa fa-share"></i>
                            <?=__(on($action === 'assigned', 'Re') . 'Assign Selected Customers')?>
                        </button>
                        <? endif; ?>
                     </div>
                     <div class="col-md-8 col-sm-12">
                        <div class="pull-right">
                            <strong><?=__('Sort')?>:</strong>
                            <select name="col" class="form-control input-inline" onchange="location='<?=url_param(null, array('dir' => $sortDirect, 'col' => null))?>&col=' + this.value">
                                <? foreach ($sortColumns as $col => $txt): ?>
                                <option value="<?=$col?>" <?=on($sortColumn === $col, 'selected')?>><?=__($txt)?></option>
                                <? endforeach; ?>
                            </select>
                            <select name="dir" class="form-control input-inline" onchange="location='<?=url_param(null, array('dir' => null, 'col' => $sortColumn))?>&dir=' + this.value">
                                <option value="ASC"  <?=on($sortDirect === 'ASC',  'selected')?>><?=__('ASC')?></option>
                                <option value="DESC" <?=on($sortDirect === 'DESC', 'selected')?>><?=__('DESC')?></option>
                            </select>
                        </div>
                     </div>
                </div>
                <table class="table table-striped table-bordered table-hover <?=on(count($columns) > 12, 'table-condensed')?> margin-bottom-10" id="search-result">
                    <thead>
                        <tr class="heading">
                            <? if (in_array('assign', $actions)): ?>
                                <th>
                                    <input type="checkbox" class="group-checkable" data-target="#search-result [name^=customers]" />
                                </th>
                            <? endif; ?>
                            <? if (in_array('nickname', $columns)): ?><th><?=__('Nickname')?></th><? endif; ?>
                            <? if (in_array('type_id', $columns)): ?><th><?=__('Type')?></th><? endif; ?>
                            <? if (in_array('email', $columns)): ?><th>Email</th><? endif; ?>
                            <? if (in_array('mobile', $columns)): ?><th><?=__('Mobile')?></th><? endif; ?>
                            <? if (in_array('phone', $columns)): ?><th><?=__('Phone')?></th><? endif; ?>
                            <? if (in_array('country_id', $columns)): ?><th><?=__('Country')?></th><? endif; ?>
                            <? if (in_array('reg_time', $columns)): ?><th><?=__('Register At')?></th><? endif; ?>
                            <? if (in_array('merchant_id', $columns)): ?><th>IB</th><? endif; ?>
                            <? if (in_array('web_uid', $columns)): ?><th>UID</th><? endif; ?>
                            <? if (in_array('mt4_demo', $columns)): ?><th>DEMO</th><? endif; ?>
                            <? if (in_array('mt4_live', $columns)): ?><th>LIVE</th><? endif; ?>
                            <? if (in_array('last_followed', $columns)): ?><th><?=__('Last Followed')?></th><? endif; ?>
                            <? if (in_array('creator', $columns)): ?><th><?=__('Creator')?></th><? endif; ?>
                            <? if (in_array('manager_user_id', $columns)): ?><th><?=__('Leader')?></th><? endif; ?>
                            <? if (in_array('sales_user_id', $columns)): ?><th><?=__('Sales')?></th><? endif; ?>
                            <th><?=__('Actions')?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <? foreach ($page->items as $item): ?>
                        <tr>
                            <? if (in_array('assign', $actions)): ?>
                                <td>
                                    <input type="checkbox" name="customers[]" value="<?=$item->customer_id?>" />
                                </td>
                            <? endif; ?>
                            <? if (in_array('nickname', $columns)): ?>
                                <td><?=highlight_keyword($item->nickname, $filter['nickname'])?></td>
                            <? endif; ?>
                            <? if (in_array('type_id', $columns)): ?>
                                <td><?=OaCustomerTypes::getName($item->type_id, $lang)?></td>
                            <? endif; ?>
                            <? if (in_array('email', $columns)): ?>
                                <td>
                                    <small>
                                        <?=highlight_keyword($item->email, $filter['email'])?>
                                    </small>
                                </td>
                            <? endif; ?>
                            <? if (in_array('mobile', $columns)): ?>
                                <td>
                                    <small>
                                        <?=on($item->mobile, highlight_keyword($item->mobile, $filter['mobile']), '-')?>
                                    </small>
                                </td>
                            <? endif; ?>
                            <? if (in_array('phone', $columns)): ?>
                                <td>
                                    <small>
                                        <?=on($item->phone, highlight_keyword($item->phone, $filter['phone']), '-')?>
                                    </small>
                                </td>
                            <? endif; ?>
                            <? if (in_array('country_id', $columns)): ?>
                                <td><?=on($item->country_id, OaCountries::getName($item->country_id), '-')?></td>
                            <? endif; ?>
                            <? if (in_array('reg_time', $columns)): ?>
                                <td>
                                    <small>
                                        <?=date('Y/m/d H:i', $item->reg_time)?>
                                    </small>
                                </td>
                            <? endif; ?>
                            <? if (in_array('merchant_id', $columns)): ?>
                                <td>
                                    <small>
                                        <?=on($item->merchant_id, highlight_keyword($item->merchant_id, $filter['merchant_id']), '-')?>
                                    </small>
                                </td>
                            <? endif; ?>
                            <? if (in_array('web_uid', $columns)): ?>
                                <td>
                                    <small>
                                        <?=on($item->web_uid, highlight_keyword($item->web_uid, $filter['web_uid']), '-')?>
                                    </small>
                                </td>
                            <? endif; ?>
                            <? if (in_array('mt4_demo', $columns)): ?>
                                <td>
                                    <small>
                                        <?=on($item->mt4_demo, highlight_keyword($item->mt4_demo, $filter['mt4_demo']), '-')?>
                                    </small>
                                </td>
                            <? endif; ?>
                            <? if (in_array('mt4_live', $columns)): ?>
                                <td>
                                    <small>
                                        <?=on($item->mt4_live, highlight_keyword($item->mt4_live, $filter['mt4_live']), '-')?>
                                    </small>
                                </td>
                            <? endif; ?>
                            <? if (in_array('last_followed', $columns)): ?>
                                <td>
                                    <small>
                                        <?=on($item->last_followed, date('Y/m/d H:i', $item->last_followed), '-')?>
                                    </small>
                                </td>
                            <? endif; ?>
                            <? if (in_array('creator', $columns)): ?>
                                <td><?=on($item->creator, OaUsers::getRealName($item->creator), '-')?></td>
                            <? endif; ?>
                            <? if (in_array('manager_user_id', $columns)): ?>
                                <td><?=on($item->manager_user_id, OaUsers::getRealName($item->manager_user_id), '-')?></td>
                            <? endif; ?>
                            <? if (in_array('sales_user_id', $columns)): ?>
                                <td><?=on($item->sales_user_id, OaUsers::getRealName($item->sales_user_id), '-')?></td>
                            <? endif; ?>
                            <td>
                                <? if (in_array('modify', $actions)): ?>
                                    <a href="<?=url('customer/modify/'.$item->customer_id)?>" class="btn btn-xs default" data-toggle="modal" data-target="#customer-modify-modal">
                                        <i class="fa fa-pencil"></i> <?=__('Modify')?>
                                    </a>
                                <? endif; ?>
                                <? if (in_array('follow', $actions)): ?>
                                    <a href="<?=url('customer/follow/'.$item->customer_id)?>" class="btn btn-xs default" data-toggle="modal" data-target="#customer-follow-modal">
                                        <i class="fa fa-phone"></i> <?=__('Follow')?>
                                    </a>
                                <? endif; ?>
                            </td>
                        </tr>
                        <? endforeach; ?>
                    </tbody>
                </table>
                <? partial_view('pagination/detail') ?>
            </div>
        </div>
    </div>
</div>

<div id="customer-assign-modal" class="modal fade" data-cache="off" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content"></div>
    </div>
</div>

<div id="customer-modify-modal" class="modal fade" data-cache="off" role="dialog" aria-hidden="true" data-backdrop="static" style="top:0;">
    <div class="modal-dialog" style="width:800px;">
        <div class="modal-content"></div>
    </div>
</div>

<div id="customer-follow-modal" class="modal fade" data-cache="off" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" style="width:750px;">
        <div class="modal-content"></div>
    </div>
</div>
